import os
import shutil
import common.gyblog as log
import common.file_operate as f_op

LOG = log.get_logger()

PATH = 'D:\迅雷下载\\'
GOAL_PATH = 'D:\workdir_2\\'


def move_xunlei(path):
    root_dirs = os.listdir(path)

    for root_dir in root_dirs:
        if os.path.isdir(path + root_dir):
            print('{}是文件夹'.format(root_dir))
            dirs = os.listdir(path + root_dir)
            flag = 0
            for d in dirs:
                if d[-4:] == 'xltd':
                    flag = 1
                    print('{}还在下载'.format(d))
            print(flag)
            if flag == 0:
                old_name = path + root_dir
                new_name = GOAL_PATH
                file_list = os.listdir(old_name)
                for file in file_list:
                    source_file = os.path.join(old_name, file)
                    target_file = os.path.join(new_name, file)
                    # 判断如果是文件则进行移动操作
                    if os.path.isfile(source_file):
                        shutil.move(source_file, target_file)
                        print('移动：{}，{}'.format(source_file, target_file))
        else:
            print('{}是文件'.format(root_dir))


if __name__ == '__main__':
    move_xunlei(PATH)
    f_op.delete_empty_dir(PATH)
